<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Markdown 编辑器 - 在线工具集合</title>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/atom-one-dark.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
            position: relative;
            overflow-x: hidden;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 30px;
            position: relative;
            z-index: 2;
        }

        .header {
            text-align: center;
            margin-bottom: 50px;
        }

        .header h1 {
            font-size: 2.6em;
            margin-bottom: 15px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: 0.5px;
        }

        .header p {
            color: rgba(255, 255, 255, 0.85);
            font-size: 1.15em;
        }

        .editor-section {
            display: grid;
            grid-template-columns: 280px 1.2fr 0.8fr;
            gap: 30px;
            background: rgba(255, 255, 255, 0.06);
            border-radius: 30px;
            padding: 40px;
            margin-bottom: 40px;
            border: 1px solid rgba(255, 255, 255, 0.12);
            backdrop-filter: blur(12px);
            box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
        }

        .sidebar {
            background: rgba(255, 255, 255, 0.06);
            border-radius: 20px;
            padding: 25px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            overflow-y: auto;
        }

        .syntax-item {
            margin-bottom: 25px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
            padding: 18px;
        }

        .syntax-title {
            color: #00b4d8;
            font-weight: 600;
            margin-bottom: 12px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .syntax-example {
            background: rgba(0, 0, 0, 0.3);
            padding: 15px;
            border-radius: 8px;
            font-family: monospace;
            font-size: 0.9em;
            white-space: pre-wrap;
            margin: 10px 0;
            line-height: 1.5;
        }

        .file-upload-wrapper {
            margin-bottom: 25px;
        }

        .file-upload-label {
            display: block;
            padding: 12px 25px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            border-radius: 10px;
            cursor: pointer;
            text-align: center;
            font-weight: 600;
            transition: transform 0.2s;
        }

        .file-upload-label:hover {
            transform: translateY(-2px);
        }

        #fileInput {
            display: none;
        }

        .official-link {
            display: block;
            margin-top: 20px;
            color: #00ff88;
            text-decoration: none;
            font-size: 0.9em;
            transition: opacity 0.3s;
        }

        .official-link:hover {
            opacity: 0.9;
            text-decoration: underline;
        }

        .editor-area, .preview-area {
            background: rgba(255, 255, 255, 0.07);
            border-radius: 20px;
            padding: 25px;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }

        .editor-area textarea {
            width: 100%;
            height: 650px;
            background: transparent;
            border: none;
            color: #fff;
            font-size: 15px;
            line-height: 1.8;
            resize: none;
            outline: none;
            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace;
        }

        .preview-area {
            overflow-y: auto;
            max-height: 1000px;
            max-width: 600px;
        }

        .preview-content {
            color: #fff;
            line-height: 1.7;
            font-size: 16px;
        }

        .preview-content h1 {
            font-size: 2.2em;
            margin: 1.6em 0 0.8em;
            color: #00ff88;
        }

        .preview-content h2 {
            font-size: 1.8em;
            margin: 1.4em 0 0.7em;
        }

        .preview-content p {
            margin: 0.8em 0;
            text-align: justify;
        }

        .preview-content code {
            background: rgba(255, 255, 255, 0.1);
            padding: 0.25em 0.45em;
            border-radius: 4px;
            font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace;
        }

        .preview-content pre {
            background: rgba(0, 0, 0, 0.35);
            padding: 1.2em;
            border-radius: 10px;
            overflow-x: auto;
            margin: 1.2em 0;
            line-height: 1.6;
        }

        .preview-content blockquote {
            border-left: 4px solid #00ff88;
            padding-left: 1.2em;
            margin: 1.2em 0;
            color: rgba(255, 255, 255, 0.85);
        }

        .preview-content ul,
        .preview-content ol {
            margin: 0.8em 0;
            padding-left: 2.5em;
        }

        .preview-content li {
            margin: 0.4em 0;
        }

        .preview-content a {
            color: #00b4d8;
            transition: opacity 0.3s;
        }

        .preview-content a:hover {
            opacity: 0.9;
            text-decoration: underline;
        }

        .preview-content img {
            max-width: 100%;
            border-radius: 10px;
            margin: 1.2em 0;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }

        .action-buttons {
            display: flex;
            gap: 30px;
            justify-content: center;
            margin-top: 40px;
        }

        .action-btn {
            padding: 16px 35px;
            border: none;
            border-radius: 15px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1.05em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 20px rgba(0, 255, 136, 0.25);
            letter-spacing: 0.3px;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 255, 136, 0.35);
        }

        .back-link {
            display: inline-block;
            margin-bottom: 25px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        @media (max-width: 1200px) {
            .editor-section {
                grid-template-columns: 260px 1fr;
            }
            .preview-area {
                display: none;
            }
        }

        @media (max-width: 992px) {
            .editor-section {
                grid-template-columns: 1fr;
            }
            .sidebar {
                display: none;
            }
            .editor-area textarea {
                height: 500px;
            }
            .preview-area {
                max-height: 500px;
                display: block;
            }
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px;
            }
            .editor-section {
                padding: 25px;
            }
            .header h1 {
                font-size: 2.2em;
            }
            .action-buttons {
                flex-direction: column;
                gap: 20px;
            }
            .action-btn {
                width: 100%;
                padding: 14px 25px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>Markdown 编辑器</h1>
            <p>支持文件上传和实时预览的专业编辑器</p>
        </div>

        <div class="editor-section">
            <!-- 左侧功能侧边栏 -->
            <div class="sidebar">
                <div class="file-upload-wrapper">
                    <input type="file" id="fileInput" accept=".md,.txt">
                    <label class="file-upload-label" for="fileInput">📁 上传文件(md,txt)</label>
                </div>

                <div class="syntax-item">
                    <div class="syntax-title">📌 标题</div>
                    <div class="syntax-example"># H1标题
## H2标题
### H3标题</div>
                </div>

                <div class="syntax-item">
                    <div class="syntax-title">🎨 文本样式</div>
                    <div class="syntax-example">**粗体文本**
*斜体文本*
~~删除线~~
`行内代码`</div>
                </div>

                <div class="syntax-item">
                    <div class="syntax-title">📋 列表</div>
                    <div class="syntax-example">- 无序列表
1. 有序列表
2. 第二项</div>
                </div>

                <div class="syntax-item">
                    <div class="syntax-title">🔗 链接与图片</div>
                    <div class="syntax-example">[链接文本](url)
![图片描述](image.jpg)</div>
                </div>

                <div class="syntax-item">
                    <div class="syntax-title">📦 代码块</div>
                    <div class="syntax-example">```javascript
function example() {
    console.log('代码块');
}
```</div>
                </div>

                <a href="https://commonmark.org/help/" target="_blank" class="official-link">
                    📚 查看完整语法指南 →
                </a>
            </div>

            <!-- 编辑区域 -->
            <div class="editor-area">
                <textarea id="editor" placeholder="在此输入 Markdown 内容..."></textarea>
            </div>

            <!-- 预览区域 -->
            <div class="preview-area">
                <div class="preview-content" id="preview"></div>
            </div>
        </div>

        <div class="action-buttons">
            <button class="action-btn" id="copyBtn">📋 复制内容</button>
            <button class="action-btn" id="downloadBtn">⤓ 下载文件</button>
        </div>
    </div>

    <script>
        // 文件上传处理
        document.getElementById('fileInput').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (!file) return;

            // 文件大小限制（2MB）
            if (file.size > 2 * 1024 * 1024) {
                toast.error('文件大小超过2MB限制');
                return;
            }

            const reader = new FileReader();
            reader.onload = function(e) {
                editor.value = e.target.result;
                editor.dispatchEvent(new Event('input'));
                toast.success(`已加载文件: ${file.name}`);
            };
            reader.onerror = () => toast.error('文件读取失败');
            
            // 检查文件类型
            if (file.type.startsWith('text/') || file.name.endsWith('.md')) {
                reader.readAsText(file);
            } else {
                toast.error('仅支持文本和Markdown文件');
            }
        });

        // Markdown解析配置
        const editor = document.getElementById('editor');
        const preview = document.getElementById('preview');
        
        marked.setOptions({
            highlight: function(code, lang) {
                if (lang && hljs.getLanguage(lang)) {
                    return hljs.highlight(code, { language: lang }).value;
                }
                return hljs.highlightAuto(code).value;
            },
            breaks: true,
            gfm: true
        });

        // 实时预览
        editor.addEventListener('input', () => {
            preview.innerHTML = marked.parse(editor.value);
        });

        // 复制功能
        document.getElementById('copyBtn').addEventListener('click', () => {
            navigator.clipboard.writeText(editor.value)
                .then(() => toast.success('内容已复制'))
                .catch(() => toast.error('复制失败'));
        });

        // 下载功能
        document.getElementById('downloadBtn').addEventListener('click', () => {
            const blob = new Blob([editor.value], { type: 'text/markdown' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = `document_${Date.now()}.md`;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
            URL.revokeObjectURL(url);
            toast.success('文件开始下载');
        });

        // 初始化示例内容
        editor.value = `# 欢迎使用Markdown编辑器

## 功能指南

### 主要特性
- 左侧面板提供常用语法参考
- 支持文件上传（.md/.txt）
- 实时预览渲染效果
- 代码语法高亮支持

\`\`\`javascript
// 示例代码
function showFeatures() {
    console.log('✔️ 实时预览');
    console.log('✔️ 文件操作');
    console.log('✔️ 语法高亮');
}
\`\`\`

> 小贴士：点击左侧的"查看完整语法指南"链接了解更多Markdown用法

![示例图片](https://via.placeholder.com/600x200?text=Markdown+Editor)
`;

        // 初始化渲染
        editor.dispatchEvent(new Event('input'));
    </script>
</body>
</html>